<?php
class AdsLocationsController extends LilAdsAppController {
    var $name = 'AdsLocations';
/**
 * beforeFilter method
 *
 * @access public
 * @return void
 */
	function beforeFilter() {
		parent::beforeFilter();
		if (!empty($this->Auth)) $this->Auth->allow('show');
	}
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		
		// plugins can also change authorization
		$c = $this->callPluginHandlers('isAuthorizedLilAds', array('return' => true, 'continue' => true));
		if (!isset($c['return']) || $c['return'] == false) return false;
		
		// plugin can determine if it wants to end authorization check
		if (!isset($c['continue']) || $c['continue'] == true) {
			if (in_array($this->params['action'], array('show'))) return true;

			return $this->isAdmin();
		}
		return true;
	}
	
	function show($location_id = null) {
		$this->layout = false;
		
		// must not exit with 404 because of possible nesting errors
    	if (!is_numeric($location_id)) return false;
    	
    	if (Configure::read('LilAds.active') && ($BannerModel = ClassRegistry::init('LilAds.Banner'))) {
    		$banner = $BannerModel->findForLocation($location_id);
			$this->set(compact('banner'));
		}
    }
    
    function admin_index() {
    	$this->set('locations', $this->paginate());
    }
    
    function admin_add() {
    	$this->setAction('admin_edit');
    }
    
   	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->AdsLocation->save($this->data)) {
				$this->Session->setFlash(__('Location has been saved.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Please verify that the information is correct.', true), 'error');
			}
		}
		if (is_numeric($id)) {
			$this->data = $this->AdsLocation->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $this->AdsLocation->delete($id)) {
			$this->Session->setFlash(__('Location has been deleted.', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->error404();
		}
	}
}
?>